package org.reliablesource.prism.gef.editor.model;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.viewers.ICellEditorValidator;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;
import org.reliablesource.prism.core.IPrismElement;
import org.reliablesource.prism.core.internal.ModelElement;

/**
 * 
 * PointerLocation.java Copyright (c) 2006, Reliable Source, Inc. All Rights
 * Reserved
 * 
 * Created on: Apr 12, 2006
 * 
 * @author Dennis Park <a
 *        <a href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 * 
 */

public class PointerLocation extends ModelElement implements IPropertySource {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2064845543908505051L;

	/**
	 * A static array of property descriptors. There is one IPropertyDescriptor
	 * entry per editable property.
	 * 
	 * @see #getPropertyDescriptors()
	 * @see #getPropertyValue(Object)
	 * @see #setPropertyValue(Object, Object)
	 */
	private static IPropertyDescriptor[] descriptors;

	/** Property ID to use when the location of this PointerLocation is modified. */
	public static final String LOCATION_PROP = "PointerLocation.Location";

	/** Property ID to use then the size of this PointerLocation is modified. */
	public static final String SIZE_PROP = "PointerLocation.Size";

	/** Property ID to use when the list of outgoing connections is modified. */
	public static final String SOURCE_CONNECTIONS_PROP = "PointerLocation.SourceConn";

	/** Property ID to use when the list of incoming connections is modified. */
	public static final String TARGET_CONNECTIONS_PROP = "PointerLocation.TargetConn";

	/**
	 * ID for the Width property value (used for by the corresponding property
	 * descriptor).
	 */
	private static final String WIDTH_PROP = "PointerLocation.Width";

	/**
	 * ID for the Height property value (used for by the corresponding property
	 * descriptor).
	 */
	private static final String HEIGHT_PROP = "PointerLocation.Height";

	/**
	 * ID for the X property value (used for by the corresponding property
	 * descriptor).
	 */
	private static final String XPOS_PROP = "PointerLocation.xPos";

	/**
	 * ID for the Y property value (used for by the corresponding property
	 * descriptor).
	 */
	private static final String YPOS_PROP = "PointerLocation.yPos";

	/** List of outgoing Connections. */
	private List sourceWireConnections;

	/** List of incoming Connections. */
	private List targetWireConnections;

	private java.awt.Point location;

	private Dimension size;

	private PointerLocation() {
		super();
	}

	public PointerLocation(final Point p, final Dimension _size) {
		this();
		location = new java.awt.Point(p.x, p.y);
		size = _size;
		sourceWireConnections = new ArrayList();
		targetWireConnections = new ArrayList();
	}

	/*
	 * Initializes the property descriptors array.
	 * 
	 * @see #getPropertyDescriptors()
	 * @see #getPropertyValue(Object)
	 * @see #setPropertyValue(Object, Object)
	 */
	static {
		descriptors = new IPropertyDescriptor[] {
				new TextPropertyDescriptor(XPOS_PROP, "X"), // id and
				// description pair
				new TextPropertyDescriptor(YPOS_PROP, "Y"),
				new TextPropertyDescriptor(WIDTH_PROP, "Width"),
				new TextPropertyDescriptor(HEIGHT_PROP, "Height"), };
		// use a custom cell editor validator for all four array entries
		for (int i = 0; i < descriptors.length; i++) {
			((PropertyDescriptor) descriptors[i])
					.setValidator(new ICellEditorValidator() {
						public String isValid(Object value) {
							int intValue = -1;
							try {
								intValue = Integer.parseInt((String) value);
							} catch (NumberFormatException exc) {
								return "Not a number";
							}
							return (intValue >= 0) ? null
									: "Value must be >=  0";
						}
					});
		}
	} // static

	/**
	 * Add an incoming or outgoing connection to this shape.
	 * 
	 * @param conn
	 *            a non-null connection instance
	 * @throws IllegalArgumentException
	 *             if the connection is null or has not distinct endpoints
	 */
	void addConnection(final Wire conn) {
		if (conn == null || conn.getSource() == conn.getTarget()) {
			throw new IllegalArgumentException();
		}
		if (conn.getSource() == this) {
			sourceWireConnections.add(conn);
			firePropertyChange(SOURCE_CONNECTIONS_PROP, null, conn);
		} else if (conn.getTarget() == this) {
			targetWireConnections.add(conn);
			firePropertyChange(TARGET_CONNECTIONS_PROP, null, conn);
		}
	}

	/**
	 * Return a pictogram (small icon) describing this model element. Children
	 * should override this method and return an appropriate Image.
	 * 
	 * @return a 16x16 Image or null
	 */
	public Image getIcon() {
		return null;
	}

	/**
	 * Return the Location of this PointerLocation.
	 * 
	 * @return a non-null location instance
	 */
	public Point getLocation() {
		return new Point(location.x, location.y);
	}
	
	public java.awt.Point getJLocation(){
		return location;
	}

	/**
	 * Returns an array of IPropertyDescriptors for this PointerLocation.
	 * <p>
	 * The returned array is used to fill the property view, when the edit-part
	 * corresponding to this model element is selected.
	 * </p>
	 * 
	 * @see #descriptors
	 * @see #getPropertyValue(Object)
	 * @see #setPropertyValue(Object, Object)
	 */
	public IPropertyDescriptor[] getPropertyDescriptors() {
		return descriptors;
	}

	/**
	 * Return the property value for the given propertyId, or null.
	 * <p>
	 * The property view uses the IDs from the IPropertyDescriptors array to
	 * obtain the value of the corresponding properties.
	 * </p>
	 * 
	 * @see #descriptors
	 * @see #getPropertyDescriptors()
	 */
	public Object getPropertyValue(final Object propertyId) {
		if (XPOS_PROP.equals(propertyId))
			return Integer.toString(location.x);
		if (YPOS_PROP.equals(propertyId))
			return Integer.toString(location.y);
		if (HEIGHT_PROP.equals(propertyId))
			return Integer.toString(size.height);
		if (WIDTH_PROP.equals(propertyId))
			return Integer.toString(size.width);
		return super.getPropertyValue(propertyId);
	}

	/**
	 * Return the Size of this PointerLocation.
	 * 
	 * @return a non-null Dimension instance
	 */
	public Dimension getSize() {
		return size.getCopy();
	}

	/**
	 * Return a List of outgoing Connections.
	 */
	public List getSourceWireConnections() {
		return new ArrayList(sourceWireConnections);
	}

	/**
	 * Return a List of incoming Connections.
	 */
	public List getTargetWireConnections() {
		return new ArrayList(targetWireConnections);
	}

	/**
	 * Remove an incoming or outgoing connection from this PointerLocation.
	 * 
	 * @param conn
	 *            a non-null connection instance
	 * @throws IllegalArgumentException
	 *             if the parameter is null
	 */
	void removeConnection(final Wire conn) {
		if (conn == null) {
			throw new IllegalArgumentException();
		}
		if (conn.getSource() == this) {
			sourceWireConnections.remove(conn);
			firePropertyChange(SOURCE_CONNECTIONS_PROP, null, conn);
		} else if (conn.getTarget() == this) {
			targetWireConnections.remove(conn);
			firePropertyChange(TARGET_CONNECTIONS_PROP, null, conn);
		}
	}

	/**
	 * Set the Location of this PointerLocation.
	 * 
	 * @param newLocation
	 *            a non-null Point instance
	 * @throws IllegalArgumentException
	 *             if the parameter is null
	 */
	public void setLocation(final Point newLocation) {
		if (newLocation == null) {
			throw new IllegalArgumentException();
		}
		location.setLocation(newLocation.x, newLocation.y);
		firePropertyChange(LOCATION_PROP, null, location);
	}

	/**
	 * Set the property value for the given property id. If no matching id is
	 * found, the call is forwarded to the superclass.
	 * <p>
	 * The property view uses the IDs from the IPropertyDescriptors array to set
	 * the values of the corresponding properties.
	 * </p>
	 * 
	 * @see #descriptors
	 * @see #getPropertyDescriptors()
	 */
	public void setPropertyValue(Object propertyId, Object value) {
		if (XPOS_PROP.equals(propertyId)) {
			int x = Integer.parseInt((String) value);
			setLocation(new Point(x, location.y));
		} else if (YPOS_PROP.equals(propertyId)) {
			int y = Integer.parseInt((String) value);
			setLocation(new Point(location.x, y));
		} 
//		else if (HEIGHT_PROP.equals(propertyId)) {
//			int height = Integer.parseInt((String) value);
//			setSize(new Dimension(size.width, height));
//		} else if (WIDTH_PROP.equals(propertyId)) {
//			int width = Integer.parseInt((String) value);
//			setSize(new Dimension(width, size.height));
//		} 
		else {
			super.setPropertyValue(propertyId, value);
		}
	}

	/**
	 * Set the Size of this PointerLocation. Will not modify the size if newSize
	 * is null.
	 * 
	 * @param newSize
	 *            a non-null Dimension instance or null
	 */
	public void setSize(Dimension newSize) {
		if (newSize != null) {
			size.setSize(newSize);
			firePropertyChange(SIZE_PROP, null, size);
		}
	}

	public void setName(String name) {
	}

	public String getName() {
		return null;
	}

	public IPrismElement getParent() {
		return null;
	}

	public Integer getId() {
		return null;
	}
}